package com.jmm.neditor.utils.template;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;

import java.io.*;

/**
 * HTMLUtil class
 *
 * @author 蒋时华
 * @date 2018-5-9
 */
public class HTMLUtil {

    /**
     * 读取指定路径文本文件
     * @param filePath
     * @return
     * @throws Exception
     */
    public static String read(String filePath) throws Exception {
        return FileUtils.readFileToString(new File(filePath), "UTF-8");
    }

    /**
     * 写入指定的文本文件，append为true表示追加，false表示重头开始写，
     * text是要写入的文本字符串，text为null时直接返回
     * @param filePath
     * @param append
     * @param context
     * @throws Exception
     */
    public static void write(String filePath, boolean append, String context) throws Exception {
        if (StringUtils.isEmpty(context)){
            return;
        }
        context = StringEscapeUtils.unescapeHtml4(context);
        if(append){
            FileUtils.writeStringToFile(new File(filePath), context, "UTF-8", true);
        }else{
            FileUtils.write(new File(filePath), context, "UTF-8", true);
        }
    }

    public static boolean isFolderExist(String path,boolean isCreate){

        File file =new File(path);
        if  (!file.exists()  && !file.isDirectory())
        {
            //如果文件夹不存在则创建
            if(isCreate){
                file.mkdir();
            }
            return false;
        } else{
            return true;
        }

    }

}
